package com.example.zhongcao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.zhongcao.entity.Question;
import com.example.zhongcao.entity.User;
import com.example.zhongcao.exception.InvalidParam;
import com.example.zhongcao.exception.User.InvalidOpenid;
import com.example.zhongcao.mapper.QuestionMapper;
import com.example.zhongcao.mapper.UserMapper;
import com.example.zhongcao.service.UserService;


import com.example.zhongcao.utils.FileUtil;
import com.example.zhongcao.utils.HttpRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    private static final String grantType="authorization_code";
    @Resource
    UserMapper userMapper;
    @Resource
    QuestionMapper questionMapper;
    private FileUtil fileUtil=new FileUtil();
    @Override
    public JSONObject codeToOpenid(String appid, String appsecret, String code) throws IOException, InvalidOpenid {
        String params="appid=" + appid + "&secret=" + appsecret + "&js_code=" + code + "&grant_type=" + grantType;
        String res=HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session",params);
        JSONObject jsonObject =  JSONObject.parseObject(res);
        String errcode=jsonObject.getString("errcode");
        User user=userMapper.selectByAppid(appid);
        if(errcode==null){
            //return jsonObject.getString("openid");
            if(user==null){
                register(appid,appsecret,jsonObject.getString("openid"));
            }
            return jsonObject;
        }else{
            if(!errcode.equals("0"))
            {
                throw new InvalidOpenid(jsonObject.getString("errmsg"));
            }else{
                //return jsonObject.getString("openid");
                if(user==null){
                    register(appid,appsecret,jsonObject.getString("openid"));
                }
                return jsonObject;
            }
        }
    }

    @Override
    public void sendQuestion(String openid, String question, List<MultipartFile> pics) throws InvalidParam {
        User user=userMapper.selectByOpenid(openid);
        if(user==null){
            throw new InvalidParam("openid无效");
        }
        if(question==null || question.equals("")){
            if(pics.size()<=0){
                throw new InvalidParam("至少说点什么吧");
            }
        }
        questionMapper.insertQuestion(openid,question);
        if(pics.size()>0){
            try {
                insertPics(pics,openid,question);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void register(String appid, String appsecret, String openid) {
        User user=new User();
        user.setAppid(appid);
        user.setAppsecret(appsecret);
        user.setOpenid(openid);
        userMapper.insertUser(user);
    }
    private void insertPics(List<MultipartFile> pics,String openid,String question) throws Exception {
        Question q=questionMapper.selectByOpenidAndQuestion(openid,question);
        String title=q.getId().toString();
        for (MultipartFile pic:pics) {
            String dir=openid+"/"+"问题反馈"+"/"+title;
            String path=fileUtil.updatePics(pic,dir);
            questionMapper.insertQuesPic(path,q.getId());
        }
    }
}
